 var gulp = require('gulp');
var gutil = require('gulp-util');
var bower = require('bower');
var concat = require('gulp-concat');
var sass = require('gulp-sass');
var minifyCss = require('gulp-minify-css');
var rename = require('gulp-rename');
var sh = require('shelljs');
var inject = require('gulp-inject'),
    browserSync = require('browser-sync'),
    util = require('util'),
    clean = require('gulp-clean'),
    uglify = require('gulp-uglify');

var paths = {
    sass: ['./scss/**/*.scss'],
    wwwd: ['www/js/**/*', '!www/css/**/*']
};

var dist = {
    css: [''],
    js: ['']
};

function browserSyncInit(baseDir, browser) {
    browser = browser === undefined ? 'default' : browser;

    var routes = null;
    if (util.isArray(baseDir)) {
        routes = {
            '/bower_components': 'bower_components'
        };
    }

    var server = {
        baseDir: baseDir,
        routes: routes
    };

    browserSync.instance = browserSync.init({
        startPath: '/',
        server: server,
        browser: browser,
        ghostMode: false
    });
}

gulp.task('browser-sync', function () {
    browserSync.init({
        server: {
            baseDir: "./"
        }
    });
});

gulp.task('default', ['sass', 'index']);

gulp.task('sass', function (done) {
    gulp.src('./scss/ionic.app.scss')
        .pipe(sass())
        .on('error', sass.logError)
        .pipe(gulp.dest('./www/css/'))
        .pipe(minifyCss({
            keepSpecialComments: 0
        }))
        .pipe(rename({extname: '.min.css'}))
        .pipe(gulp.dest('./www/css/'))
        .on('end', done);
});

gulp.task('watch', ['inject_src_js'], function () {
    //gulp.watch(paths.sass, ['sass', 'inject_src_js']);
    gulp.watch(paths.wwwd, ['inject_src_js']);
});

// inject 注入
gulp.task('inject_src_js', function () {
    gulp.src('./www/index_template.html')
        .pipe(inject(gulp.src(['./www/js/**/*.js'], {
            read: false
        }), {
            relative: true
        }))
        .pipe(rename('index.html'))
        .pipe(gulp.dest('./www'));
});

gulp.task('install', ['git-check'], function () {
    return bower.commands.install()
        .on('log', function (data) {
            gutil.log('bower', gutil.colors.cyan(data.id), data.message);
        });
});

gulp.task('git-check', function (done) {
    if (!sh.which('git')) {
        console.log(
            '  ' + gutil.colors.red('Git is not installed.'),
            '\n  Git, the version control system, is required to download Ionic.',
            '\n  Download git here:', gutil.colors.cyan('http://git-scm.com/downloads') + '.',
            '\n  Once git is installed, run \'' + gutil.colors.cyan('gulp install') + '\' again.'
        );
        process.exit(1);
    }
    done();
});

gulp.task('serve', ['watch'], function () {
    browserSyncInit(['./www']);
});

// 清理
gulp.task('clean', function () {
    return gulp.src(['./www/assets', './www/index.html'], {
        read: false
    }).pipe(clean());
});

// 脚本
gulp.task('scripts', function () {
    return gulp.src('./www/js/**/*.js')
        .pipe(concat('app.bound.js'))
        .pipe(gulp.dest('./www/assets/js'))
        .pipe(rename({
            suffix: '.min'
        }))
        .pipe(uglify())
        .pipe(gulp.dest('./www/assets/js'));
});
